<?php
/* Author: Alex Fu
   Time : 2016/12/1
   Motion Global Leave Center

   Controller : User
*/
class User extends CI_Controller
{
	protected static $apikey = "a7f7ad617f2f54a6acd5d51828abcfdc";

	public function __construct()
	{
		parent::__construct();
        $this->load->helper('url_helper');
		$this->load->library('session');
		$this->load->library('encryption');
		$this->load->model("user_model");
	}

	
	
	public function loginAction()
	{ 	
		/*
		Login function Controller , FOR AJAX 
		*/

		if($this->getLoginAction())
		{
			echo json_encode(array("code"=>"101","text"=>"Logged")) ;return;
		}
		$data = $this->input->post();
		echo json_encode($this->user_model->getLoginVerifyState($data));
	}

	public function getLoginAction()
	{
		return isset($_SESSION['user_state']);
	}

	public function registerView()
	{
		if($this->getLoginAction())
		{
			$this->load->view("main");
		}
		else
		{
			$this->load->view("user/register");
		}
		
	}

	public function registerAction()
	{
		$data = $this->input->post();
		$result = $this->user_model->getRegisterState($data);
		echo json_encode($result);
	}

	public function confirmView()
	{
		if($this->user_model->registerConfirmState()['code'] == "200" )
		{
			$this->load->view("/user/confirm");
		}
		else
		{
			$this->load->view("/user/wrong",array("text"=>"The registration has overtimed, Please register Again. <br/> <a href='/register/view'>CLICK HERE</a>"));
		}
	}

	public function profileView()
	{
		if($this->getLoginAction())
		{
			$data = $this->user_model->getProfileState(array("user_id"=>$_SESSION['user_id']));
			$res = $this->user_model->getLeaveAndSickDaysState();
			$data = array_merge($data,$res);
			$this->load->view("/user/profile",array("data"=>$data));
		}
		else
		{
			echo '<script>location.href="/"</script>';
		}
	}

	public function profileEditAction()
	{
		if($this->getLoginAction())
		{
			$data = $this->input->post();
			$this->user_model->editProfile($data);
			echo json_encode(array("code"=>"200"));
		}
		else
		{
			echo json_encode(array("code"=>"101","text"=>"Please Log in"));
		}

	}

	
}


   
?>